package com.coocaa.ops.admin.v1.poster;

import com.alibaba.fastjson.JSONArray;
import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.poster.constant.PosterTypeConstants;
import com.coocaa.ops.admin.v1.poster.model.GlobalPictureModel;
import com.coocaa.ops.admin.v1.poster.service.IGlobalPictureQueryService;
import com.coocaa.ops.admin.v1.poster.service.IGlobalPictureUpdateService;
import com.coocaa.ops.admin.v1.poster.validator.PosterValidator;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author bjh
 * @description 素材图片表-前端控制器
 * @date 2020-06-13 10:50:35
 */
@RestController
@Config(menuToken = "poster")
@RequestMapping("/v1/poster")
public class GlobalPictureController extends MgrBaseController {

    @Autowired
    private IGlobalPictureUpdateService thisUpdateService;
    @Autowired
    private IGlobalPictureQueryService thisQueryService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(HttpServletRequest request, @RequestParam String jsonStr) {
        List<GlobalPictureEntity> list = JSONArray.parseArray(jsonStr, GlobalPictureEntity.class);
        thisUpdateService.saveBatch(list, WebUtil.getCurrentUser(request));
        return R.ok();
    }

    /**
     * 批量审核
     *
     * @param request
     * @param auditFlag
     * @param auditDesc
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BATCHAUDIT)
    @RequestMapping(value = "/batchAudit", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object batchAudit(HttpServletRequest request, String idStr, Integer auditFlag, String auditDesc) {
        SysUserEntity user = WebUtil.getCurrentUser(request);
        thisUpdateService.batchAudit(idStr, user, auditFlag, auditDesc);
        return R.ok().msg(commonLanguageProperties.getAuditSuccess());

    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        GlobalPictureController.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 分页数据显示-菜单自己使用
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String pageList(HttpServletRequest request, GlobalPictureModel model) {
        model.setPictureType(PosterTypeConstants.HOME);
        PageResultModel<GlobalPictureEntity> pageResult = thisQueryService.pageList(model);
        R result = R.ok(pageResult);
        return render2Json(result);

    }

    /**
     * 分页数据显示-其他处选择
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/dataList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String dataList(HttpServletRequest request, GlobalPictureModel model) {
        PageResultModel<GlobalPictureEntity> pageResult = thisQueryService.dataList(model);
        R result = R.ok(pageResult);
        return render2Json(result);
    }


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = RequestMethod.GET)
    public Object getDetailInfo(@RequestParam Integer id, @RequestParam(required = false) String version) {
        InputParamsValidator.validIdValue(id);
        GlobalPictureEntity entity = thisQueryService.getDetailInfo(id, version);
        Map result = R.ok().data(entity);
        ;
        return render2Json(result);

    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public Object remove(Integer[] id, HttpServletRequest request) {
        Assert.notEmpty(id, "删除的id不能为空！");
        return thisUpdateService.deleteBatch(id, WebUtil.getCurrentUser(request));

    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }


}
